﻿using Common.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.ViewModels
{
    public interface IMessageBoxService
    {
        public Task<MessageBoxResult> Show(string content, string title, MessageBoxButtons messageBoxButtons);
    }

    public delegate Task<MessageBoxResult> DeleShow(string content, string title, MessageBoxButtons messageBoxButtons, string Hint = "");

    public class MessageBoxService
    {
        public static List<string> MessageBoxServiceNotInitGenerateMessage = new(10);
        public static DeleShow MessageBoxShow;

        private static IMessageBoxService IMessageBoxService;

        /// <summary>
        /// 消息wei
        /// </summary>
        /// <param name="messge"></param>
        private static void NotInitGenerateMessage(string messge)
        {
            MessageBoxServiceNotInitGenerateMessage.Add(messge);
            Task.Run(async () =>
            {
                try
                {
                    while (MessageBoxServiceNotInitGenerateMessage.Count > 0)
                    {
                        if (MessageBoxShow != null)
                        {
                            foreach (var item in MessageBoxServiceNotInitGenerateMessage)
                            {
                                string[] str = item.Split('-');
                                await MessageBoxShow(str[0], str[1], MessageBoxButtons.Ok);
                                Thread.Sleep(1000);
                            }
                            MessageBoxServiceNotInitGenerateMessage.Clear();
                        }
                        Thread.Sleep(500);
                    }
                }
                catch (Exception e)
                {

                }
            });
        }

        public static Task<MessageBoxResult> Show(string content, string title, MessageBoxButtons messageBoxButtons, string Hint = "")
        {
            //不能为空
            if (MessageBoxShow == null)
            {
                NotInitGenerateMessage(content + "-" + (!string.IsNullOrWhiteSpace(title) ? title : " "));
                return null;
            }
            return MessageBoxShow(content, title, messageBoxButtons, Hint);

        }

        public static Task<MessageBoxResult> Show(string content)
        {
            return Show(content, "提示", MessageBoxButtons.Ok);
        }
    }
}
